package cn.tedu.net.collection.seri;

import java.io.*;

//本类用于测试序列化与反序列化
public class Test1_Seri {
    public static void main(String[] args) {
        method1();//测试序列化
        method2();//测试反序列化
    }

    private static void method1() {//本方法用于测试序列化
            ObjectOutputStream out=null;
        try {
            out=new ObjectOutputStream(new FileOutputStream("D:\\ready\\1.txt"));
            //完成序列化操作
            //创建对象
            Student s=new Student("迪丽热巴",29,"女","电视台");
            out.writeObject(s);
            System.out.println("序列化成功");
        } catch (IOException e) {
            System.out.println("序列化失败");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void method2() {//本方法用于测试反序列化
        ObjectInputStream in=null;
        try {
            in=new ObjectInputStream(new FileInputStream("D:\\ready\\1.txt"));
            Object o=in.readObject();
            System.out.println(o);
            System.out.println("反序列化成功");
        } catch (Exception e) {
            System.out.println("反序列化失败");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
